package cn.flt.lucia.config;

import cn.flt.lucia.Intermediate.FabricManager;
import cn.flt.lucia.Intermediate.OrgManager;
import lombok.Data;
import org.hyperledger.fabric.protos.peer.Query;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author 州长在手 2020/7/6 下午4:21
 */
@Component
@ConfigurationProperties(prefix = "flkj2")
@Data
public class EnvRunner2 implements CommandLineRunner {

    /*init*/
    private String cc;
    private String orgMSPID;

    private boolean openTLS;
    /*setUser*/
    private String userleagueName;
    private String userorgName;
    private String userpeerName;
    private String username;
    private String userskPath;
    private String usercertificatePath;
    /*addOrderer*/
    private String orderername;
    private String ordererlocation;
    private String ordererserverCrtPath;
    private String ordererclientCertPath;
    private String ordererclientKeyPath;
    /*addPeer*/
    private String peerName;
    private String peerLocation;
    private String peerEventHubLocation;
    private String peerserverCrtPath;
    private String peerclientCertPath;
    private String peerclientKeyPath;
    /*setChannel*/
    private String channelName;
    /*addChainCode*/
//    private String chaincodeName01;
//    private String chaincodePath;
//    private String GOPATH;
//    private String chaincodePolicy;
//    private String chaincodeVersion;
//    private int proposalWaitTime;

    public FabricManager fm;
    public OrgManager om;


    @Override
    public void run(String... args) {
        System.out.println(">>>>>>>>>>>>>>>服务启动执行，执行加载数据等操作<<<<<<<<<<<<<");
//        System.out.println(getCc());
        this.om = new OrgManager();
        try {
            this.om.init(getCc(),
                    getOrgMSPID(),
                    isOpenTLS());

            this.om.setUser(getUserleagueName(),
                    getUserorgName(),
                    getUserpeerName(),
                    getUsername(),
                    getUserskPath(),
                    getUsercertificatePath());

            this.om.addOrderer(getOrderername(),
                    getOrdererlocation(),
                    getOrdererserverCrtPath(),
                    getOrdererclientCertPath(),
                    getOrdererclientKeyPath());

            this.om.addPeer(getPeerName(),
                    getPeerLocation(),
                    getPeerEventHubLocation(),
                    getPeerserverCrtPath(),
                    getPeerclientCertPath(),
                    getPeerclientKeyPath());
            this.om.setChannel(getChannelName());
            // 不需要手动add
//            this.om.addChainCode(getChaincodeName(),
//                    getChaincodePath(),
//                    getGOPATH(),
//                    getChaincodePolicy(),
//                    getChaincodeName(),
//                    getProposalWaitTime());
            this.om.add();
            fm = this.om.use(getCc(), getUsername());
            /*
             *通过遍历全部实例化的链码add到om
             */
            Collection<Peer> peerCollection = fm.queryPeer();
            for (Peer peer : peerCollection) {
                List<Query.ChaincodeInfo> chaincodeInfos = fm.queryInstantiated(peer);
                for (Query.ChaincodeInfo chaincodeInfo : chaincodeInfos) {
                    //  System.out.println(chaincodeInfo.getName());
                    om.addChainCode(chaincodeInfo.getName(),
                            "",
                            "",
                            "",
                            "",
                            90000);
                }
            }
        } catch (InvalidArgumentException | ProposalException e) {
            System.out.println("『错误信息：』" + e.getMessage());
            System.out.println("----------------FM INIT FAILED & EXIT----------------");
            System.exit(0);
        }
        try {
            System.out.println(fm.getBlockchainInfo().toJSONString());
        } catch (InvalidArgumentException | ProposalException e) {
            System.out.println("『获取区块链信息错误：』" + e.getMessage());
            System.out.println("----------------FM INIT FAILED & EXIT----------------");
        }
        System.out.println("----------------FM INIT OK----------------");

    }
}
